/*          ._
 *            '.			     _______ _______ ___ ___ _______ 
 *     --._     \    .-.		|   |   |_     _|   |   |    ___|
 *         '. .--;  /  |		|       |_|   |_|   |   |    ___|
 *         ,;/ ^  |`\  /.-"".   |___|___|_______|\_____/|_______|	
 *        ;' \  _/   |'    .'          code.google.com/p/as-hive
 *       /    `.;I> /_.--.` )    __
 *      |    .'`  .'( _.Y/`;    /  \__
 *       \_.'---'`   `\  `-/`-. \__/  \__
 *         /_-.`-.._ _/`\  ;-./_/  \__/  \
 *        |   -./  ;.__.'`\  /  \__/  \__/
 *         `--'   (._ _.'`|  \__/  \__/
 *                /     ./ __/  \__/  \
 *           jgs ; `--';'_/  \__/  \__/
 *               `;-,-'/  \__/  \
 *               ,'`   \__/  \__/
 *
 * Copyright 2007 (c) Jovan Alleyne, Peter Nitsch, Brandon Flowers.
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

 /**
 * @author	Jovan Alleyne, Peter Nitsch, Brandon Flowers
 * @version	0.1
 * @since	Flash 9
 */

package org.hive
{
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.utils.getDefinitionByName;
	
	import org.hive.managers.*;
	import org.hive.data.Properties;
	import org.hive.events.PropertiesEvent;
	
	/**
	* The Hive class is the application document class and contains global settings.
	*/
	public class Hive extends Sprite
	{
		
		/**
		* Framework name.
		*/
		public static var NAME:String = 'Hive State Framework';

		/**
		* Framework version.
		*/
		public static var VERSION:String = '0.2';
		
		//________________________________________________ SETTINGS
		
		/**
		* Flag for debug mode.
		* Used to display memory graph.
		*/
		public static var APPLICATION_MODE:Number = 0;
		
		/**
		* Flag for language mode.
		* 0 - Use "en" (English) as default.
		* 1 - Use the lng parameter from the URL.
		*/
		public static var LANGUAGE_MODE:Number = 0;
		
		//________________________________________________ ACCESSORS
		
		/**
		* Determines whether or not the properties are loaded.
		* @see		onPropertiesLoaded
		* @see		onComplete
		* @default	false
		*/
		private var __experienceLoaded:Boolean = false;
		
		/**
		* Static reference to the instance of the framework.
		*/
		private static var __instance:Hive;
		
		/**
		* Static reference to the properties.
		* @see 		org.hive.data.Properties
		*/
		public static var PROPS:Properties;
		
		/**
		*
		* @return	__instance
		*/
		public static function get STAGE( ):DisplayObject
		{
			return __instance;
		}
		
		//________________________________________________ CONSTRUCTOR
		
		/**
		* Creates an instance of the Hive framework.
		*
		* @param	propertiesFile		Properties XML file
		*/
		public function Hive( propertiesRequired:Boolean=false, propertiesFile:String=null )
		{
			super();
			
			trace(NAME +" :: "+ VERSION);
			__instance = this;
			
			if ( propertiesRequired ) {
				if ( propertiesFile != null ) {
					initProperties( propertiesFile );
				} else {
					trace( "Hive :: Manual properties initialization required." );
				}
			} else {
				loaderInfo.addEventListener(Event.COMPLETE, onComplete, false, 0, true);
			}
	
		}
		
		protected function initProperties( propertiesFile:String ):void
		{
			PROPS = new Properties( propertiesFile );
			PROPS.addEventListener( PropertiesEvent.PROPERTIES_READY, onPropertiesLoaded, false, 0, true );
			PROPS.load();
		}
		
		//________________________________________________ EVENT HANDLERS
		
		
		/**
		* Fires the loadExperience() method once properties are loaded.
		* @param	e				Triggers by properties XML loaded.
		* @see		loadExperience
		*/
		public function onPropertiesLoaded( e:PropertiesEvent ):void
		{
			if( !__experienceLoaded ){
				__experienceLoaded = true;
				loadExperience();
			}
		}
		
		private function onComplete( e:Event ):void
		{
			//Implement in own class if needed
		}
		
		public static var DOCUMENT_WIDTH	:Number;
		public static var DOCUMENT_HEIGHT	:Number;
		
		public function loadExperience():void
		{
			DOCUMENT_WIDTH = PROPS.stage_width;
			DOCUMENT_HEIGHT = PROPS.stage_height;
			
			if( stage ) {
				stage.scaleMode = PROPS.stage_scaleMode;
				stage.align = PROPS.stage_align;
			}
			
			APPLICATION_MODE = PROPS.application_mode;
			LANGUAGE_MODE = PROPS.language_mode;
			
			var ContentList:Class = getDefinitionByName( PROPS.data_model ) as Class;
			var EntryPointState:Class = getDefinitionByName( PROPS.entryState ) as Class;
			StateManager.init( new ContentList( PROPS.data_url ), new EntryPointState(), this ); 
		}
	}
}
